package test;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.util.HashMap;
import java.util.Map;

public class ZookeeperConfigSender {
    private static final int RETRY_TIMES = 3;
    private static final int SLEEP_TIME = 1000;
    private static String APP_NAME = "sentinel-zookeeper-demo";
    private static final String RULE_ROOT_PATH = "/sentinel_rule_config";
    private static final String FLOW_RULE_PATH = "/flow_rule";
    private static final String DEGRADE_RULE_PATH = "/degrade_rule";
    private static final String ZOOKEEPER_ADDRESS = "localhost:2181";

    public static void main(String[] args) throws Exception {
        Map<String, String> ruleAndPath = getRuleAndPath(2);
        String rule = ruleAndPath.get("rule");
        String path = ruleAndPath.get("path");

        CuratorFramework zkClient = CuratorFrameworkFactory.newClient(ZOOKEEPER_ADDRESS, new ExponentialBackoffRetry
                (SLEEP_TIME, RETRY_TIMES));
        zkClient.start();
        Stat stat = zkClient.checkExists().forPath(path);
        if (stat == null) {
            zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null);
        }
        zkClient.setData().forPath(path, rule.getBytes());

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        zkClient.close();
    }

    private static Map<String, String> getRuleAndPath(int type) {
        String rule = "";
        String path = "";
        switch (type) {
            case 1:
                rule = "[\n"
                        + "  {\n"
                        + "    \"resource\": \"TestResource\",\n"
                        + "    \"controlBehavior\": 0,\n"
                        + "    \"count\": 1.0,\n"
                        + "    \"grade\": 1,\n"
                        + "    \"limitApp\": \"default\",\n"
                        + "    \"strategy\": 0\n"
                        + "  }\n"
                        + "]";
                path = RULE_ROOT_PATH + FLOW_RULE_PATH + "/" + APP_NAME;
                break;
            case 2:
                rule = "[\n"
                        + "  {\n"
                        + "    \"resource\": \"TestResource\",\n"
                        + "    \"app\": \"sentinel-zookeeper-demo\",\n"
                        + "    \"ip\": \"192.168.3.61\",\n"
                        + "    \"grade\": 0,\n"
                        + "    \"count\": 1.0,\n"
                        + "    \"timeWindow\": 5,\n"
                        + "    \"minRequestAmount\": 1,\n"
                        + "    \"statIntervalMs\": 1000,\n"
                        + "    \"slowRatioThreshold\": 0\n"
                        + "  }\n"
                        + "]";
                path = RULE_ROOT_PATH + DEGRADE_RULE_PATH + "/" + APP_NAME;
                break;
            default:
        }
        Map<String, String> ruleAndPath = new HashMap<>();
        ruleAndPath.put("rule", rule);
        ruleAndPath.put("path", path);
        return ruleAndPath;
    }
}